import { Room } from "src/class/room";

class RoomManager {
    private rooms: Room[] = [];

    private kRoomIdStart: number = 10000;

    public createNewRoomId()
    {
        return this.kRoomIdStart + this.rooms.length + 1;
    }

    public getRoomById(roomId: number)
    {
        for (let key in this.rooms) {
            let room = this.rooms[key];
            if (room.id == roomId) {
                return room;
            }
        }

        return null;
    }

    public createRoom(room: Room)
    {
        this.rooms.push(room);
    }

    public dissolveRoom(roomId: number)
    {
        let index = this.rooms.findIndex((room) => {
            return room.id == roomId;
        });

        if (index == -1) {
            return;
        }

        let room = this.rooms[index];

        for (let key in room.allUser) {
            room.allUser[key].roomId = 0;
        }

        this.rooms.splice(index, 1);

    }
}

export let roomManager = new RoomManager();